<?php

namespace ChannelThrift\Controller;

require_once MODULE_PATH . 'Org/Thrift/Lib/Thrift/ClassLoader/ThriftClassLoader.php';

use ChannelThrift\Processor\BailService;
use Services\BailService\BailServiceProcessor;
use Thrift\ClassLoader\ThriftClassLoader;

$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', MODULE_PATH . 'Org/Thrift/Lib');
$loader->registerDefinition('Services', MODULE_PATH . 'Org/Thrift/gen-php');
$loader->register();

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
use Thrift\TMultiplexedProcessor;


class ServerController extends CommonController
{
    public function _initialize()
    {
        header('Content-Type', 'application/x-thrift');
    }


    public function runBail()
    {
        //数据传输方式 通信方式
        $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
        //传输格式
        $protocol = new TBinaryProtocol($transport, true, true);
        $tMultiplexedProcessor = new TMultiplexedProcessor();

        $handler = new BailService();
        $bailServiceProcessor = new BailServiceProcessor($handler);
        $tMultiplexedProcessor->registerProcessor("BailService", $bailServiceProcessor);

        $transport->open();
        $tMultiplexedProcessor->process($protocol, $protocol);
        $transport->close();


    }

    public function wz()
    {
        header("Content-Type:text/html; charset=utf-8");
        $param = array();
        $param['key'] = 'd1f3b8d823698d3a47dbec91d704ce0b';
        $param['lat'] = 40.006573;
        $param['lon'] = 116.394423;
        $param['page'] = '';
        $param['pagesize'] = '';
        $param['r'] = 2000;
        $url = "http://v.juhe.cn/wzpoints/query";
        $result = curlHttp($url, $param);
        dump(json_decode($result, true));
    }

    public function jyz()
    {
        header("Content-Type:text/html; charset=utf-8");
        $param = array();
        $param['key'] = '015518a23d1a66d216da833006435494';
        $param['lat'] = 40.006573;
        $param['lon'] = 116.394423;
        $param['page'] = '';
        $param['pagesize'] = '';
        $param['format'] = '1';
        $param['r'] = 10000;
        $url = "http://apis.juhe.cn/oil/local";
        $result = curlHttp($url, $param);
        dump(json_decode($result, true));
    }

    public function dtc()
    {
        header("Content-Type:text/html; charset=utf-8");
        $param = array();
        $param['key'] = 'b46faae3cced1c720791b37fc3241e1f';
        $param['code'] = 'P0109';
        $url = "http://getDTC.api.juhe.cn/CarManagerServer/getDTC";
        $result = curlHttp($url, $param);
        dump(json_decode($result, true));
    }

    public function stop()
    {
        header("Content-Type:text/html; charset=utf-8");
        $param = array();
        $param['key'] = '1105d063944e8f99cac7acb44b9b3d10';
        $param['JD'] = 40.006573;
        $param['WD'] = 116.394423;
        $param['SDXX'] = '';
        $param['TCCFL'] = '';
        $param['TCCLX'] = '';
        $param['JLPX'] = 1;
        $param['JLCX'] = 3000;
        $url = "http://japi.juhe.cn/park/nearPark.from";
        $result = curlHttp($url, $param);
        dump(json_decode($result, true));
    }

    public function baidu()
    {
        header("Content-Type:text/html; charset=utf-8");
        $param = array();
        $param['ak'] = 'w3yQAndtHqH2BRy22NEYj2nB8cEuFkMu';
        $param['query'] = '停车场';
        $param['page_size'] = 10;
        $param['page_num'] = 20;
        $param['scope'] = 1;
        $param['location'] = '40.006573,116.394423';
        $param['radius'] = 10000;
        $param['output'] = 'json';
        $url = "http://api.map.baidu.com/place/v2/search";
        $result = curlHttp($url, $param);
        dump(json_decode($result, true));
    }
    public function test2(){
        //echo json_encode(array('a'=>1,'b'=>2,'c'=>3,'d'=>4));
//        $a = new \ChannelThrift\Service\CarDealerService();
//        $res = $a->getJpushId(239);
//        $a = new \ChannelThrift\Service\AddressService();
//        //所在地
//        $city_name = $a->get_name_by_code(110000);

        //$carModelServ = new \ChannelThrift\Service\CarModelService();
        //$res = $carModelServ->get_model_by_id(1531);
//        $jpush_data = array('order_id'=>2276);
//        $jpush_content = '【智能报价】[杭州] 科鲁兹 2016款 1.4T 自动，拍单编号：P1611280004，当前价14.4万元，已超过您设置的智能报价上限。 ';
//        $antServ = new \ChannelThrift\Service\AntNestService();
//        $res = $antServ->jpush_msg('15921306031','120c83f7602d3291252',2,$jpush_data,$jpush_content);

        $carServ = new \ChannelThrift\Service\CarService();
        $car_info = $carServ->getCar(2086);
        dump($car_info);


        //$carModelServ->abc();
        dump($res);
    }
   public function test(){
       //echo strtotime('2016-11-28 14:05:46');
       $a  = new \ChannelThrift\Processor\AuctionService();
       //$a->bid(256, 2231, 11000,0);
       //$res = $a->intelBidStop(256, 2262);
       //$res = $a->intelBidding(256, 2265,11500,300);
       //$res = $a->getLastAutoSetInfo(256, 2265);
       //$res = $a->jpush_msg(256, 2277,30900);// $dealer_id,$order_id,$next_price

       $redis = new \Common\Org\PhpRedis(4);
        $res = $redis->get('autobidding_dealer_info_2277_256');
       $res = json_decode($res,true);
//       $res = $redis->keys('autobidding_dealer_info_2277_*');
//       foreach($res as $key){
//           $arr = $redis->get($key);//获取每一个车商的设置信息
//           $dealer = json_decode($arr,true);
//           dump($dealer);
//       }
//       echo '<hr>';




       $antServ = new \ChannelThrift\Service\AntNestService();
       $res = $a->jpush_msg($redis,256,2285,20000,$antServ);
       dump($res);

   }

    public function runBid()
    {
        //数据传输方式 通信方式
        $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
        //传输格式
        $protocol = new TBinaryProtocol($transport, true, true);
        $tMultiplexedProcessor = new TMultiplexedProcessor();

        $handler = new BailService();
        $bailServiceProcessor = new AuctionServiceProcessor($handler);
        $tMultiplexedProcessor->registerProcessor("BailService", $bailServiceProcessor);

        $transport->open();
        $tMultiplexedProcessor->process($protocol, $protocol);
        $transport->close();


    }

}